import { AK } from '@/model/const/ModelConst';
import { HoleMeta, CreateMeta } from '../../meta/MetaClass';
import AluGeoUtil from '@/model/geometry/utils/AluGeoUtil';
import AModelFmulaUtils from '@/model/utils/AModelFmulaUtils';





/** 倒角圆弧角铝*/

export default class RAluEntity {


    static __create(data: AIBasicParams): GeometryReturnMeta<Array<any>> {
        let vec = [];
        let indices = [];

        //上边
        let bb = this.__basicBoard(data);
        //下边
        let cc = this.__activeBoard(data);


        vec.push(...bb.vertices, ...cc.vertices);
        indices.push(...bb.indices, ...cc.indices.map((value) => { value += bb.vertices.length / 3; return value; }));

        let dd = this.__rcom(data);
        indices.push(...dd.indices.map((value) => { value += vec.length / 3; return value; }));
        vec.push(...dd.vertices);

        // for(let i: number = 0; i < vec.length; i+=3){
        //     vec[i] -= +data.L;
        // }
        this.setDuan(data);

        return {
            vertices: vec,
            indices: indices,
            meshface: []
        }
    }


    private static setDuan(data: AIBasicParams) {
        data["duan"] = [
            0, 0, 0,
            data.L, 0, 0,
            0, 0, -data.W,
            data.L, 0, -data.W
        ];
        data["zhong"] = {
            L: [
                {
                    pointer: [data.L, -data.H / 2, 0],
                    center: [data.L / 2, -data.H / 2, 0]
                },
            ],
            RL: [
                {
                    pointer: [0, -data.H / 2, 0],
                    center: [data.L / 2, -data.H / 2, 0]
                },
            ]
        }
    }


    /** xoz面*/
    private static __basicBoard(data: AIBasicParams): GeometryReturnMeta<Array<any>> {

        let l = +data.W, l1 = +data.H, w = +data.L, t = +data.SXKH;

        //计算一下xoz板与弧板的交点
        let q = (l1 - t) * l / l1 + t * 0.5;

        let back = [
            0, t, -t, w, t, -t, w, q, -t, 0, q, -t,
        ];

        let front = [
            0, 0, 0, w, 0, 0, w, l, 0, 0, l, 0,
        ];


        let h = new HoleMeta((+data.BKDia) * 0.5, (+data[AK.p_kbj]), AModelFmulaUtils.getHolesbyExpress(data[AK.p_skkw], w, 2, data, "p_skkw"), "x");
        let c: CreateMeta = new CreateMeta(t, front, back, h);
        let result = AluGeoUtil.createBoardWithHole(c);

        //绕x旋转
        let r = -0.5 * Math.PI;
        let sr = Math.sin(r);
        let cr = Math.cos(r);

        for (let i: number = 0; i < result.vertices.length; i += 3) {
            let y = result.vertices[i + 1];
            let z = result.vertices[i + 2];

            result.vertices[i + 1] = y * cr - sr * z;
            result.vertices[i + 2] = sr * y + z * cr;
        }

        return result;
    }

    /** xoy板*/
    private static __activeBoard(data: AIBasicParams): GeometryReturnMeta<Array<any>> {

        let l = +data.H, l1 = +data.W, w = +data.L, t = +data.SXKH;

        //计算一下xoz板与弧板的交点
        let q = (l1 - t) * l / l1;

        let back = [
            0, -q, -t, w, -q, -t, w, -t, -t, 0, -t, -t,
        ];
        let front = [
            0, -l, 0, w, -l, 0, w, 0, 0, 0, 0, 0,
        ];

        let c: CreateMeta = new CreateMeta(t, front, back, null);
        let result = AluGeoUtil.createBoardWithHole(c);

        return result;
    }

    /** 弧板
     *  l1 === xoy板的高 ==== 弧板的x
     *  l === xoz板的高 ==== 弧板的y
     *  w === 弧板的宽
     * 
    */
    private static __rcom(data: AIBasicParams): GeometryReturnMeta<Array<any>> {

        let x = +data.H, y = +data.W, w = +data.L, t = +data.SXKH, r = +data.arcR;

        let rs = AluGeoUtil.createSellipse(x, y, t, w, r);

        for (let i: number = 0; i < rs.vertices.length; i += 3) {

            rs.vertices[i + 2] *= -1;
        }

        return rs;
    }
}